package main

import (
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"github.com/xormplus/xorm"
	"time"
)

type Student struct {
	Id          int64  `param:"<in:formData>"`
	Name        string `param:"<in:formData>"`
	Password    string `param:"<in:formData>"`
	Pid         int    `param:"<in:formData>"`
	Ident       string `xorm:"VARCHAR(255)" param:"<in:formData>"`
	Points      int    `param:"<in:formData>"`
	Image       string `xorm:"VARCHAR(255)" param:"<in:formData>"`
	Create_time string `xorm:"TIMESTAMP DEFAULT 'CURRENT_TIMESTAMP'"`
	Effect      int    `xorm:"TINYINT(1) DEFAULT 1" param:"<in:formData>"`
}

type Admin struct {
	Id          int64
	Name        string `param:"<in:formData><required><len:5:20>"`
	Password    string `param:"<in:formData><required><len:5:20>"`
	Create_time string `xorm:"TIMESTAMP DEFAULT 'CURRENT_TIMESTAMP'"`
}

type Question struct {
	Id      int64          `param:"<in:formData>"`
	Desc    string         `param:"<in:formData>"`
	Option  map[int]string `XORM:"BLOB" param:"<in:formData>"`
	Correct int            `param:"<in:formData>" xorm:"INT(11) DEFAULT 0"`
	Answer  int            `param:"<in:formData>"`
	Image   string         `xorm:"VARCHAR(255)" param:"<in:formData>"`
	Reason  string         `xorm:"VARCHAR(255)" param:"<in:formData>"`
}

var engine *xorm.Engine

func ORM() {
	start := time.Now()
	var SQLerr error
	engine, SQLerr = xorm.NewEngine("mysql", "root:okayokay@tcp(www.deeokay.top:3306)/wz_exam?charset=utf8")
	if SQLerr != nil {
		fmt.Println(SQLerr.Error())
	}
	objetArr := []interface{}{
		new(Admin),
		new(Student),
		new(Question),
	}
	var finish = 0
	var channel = make(chan int)
	defer close(channel)
	for i, obj := range objetArr {
		go func(index int, object interface{}) {
			syncErr := engine.Sync2(object)
			if syncErr != nil {
				//fmt.Println(syncErr)
			} else {
				fmt.Printf("ORM同步第%d个模型完成%v\n", index, object)
			}
			finish++
			if finish == len(objetArr) {
				channel <- 0
			}
		}(i, obj)
	}
	<-channel
	fmt.Println("ORM用时:", time.Since(start))
	fmt.Println("---> 1.考试系统已成功启动")
	fmt.Println("---> 2.请在浏览器中输入: 127.0.0.1:8082 即可进入考试系统")
	fmt.Println("---> 3.若倒计时功能不正常,请清除浏览器缓存/上网痕迹,一般在 设置->工具->清除")
	fmt.Println("---> 4.网页刷新不会影响考试,但不建议这样做,此举操作会加大服务器负担")
	fmt.Println("---> 5.如发现任何问题,请联系我 QQ:652175125 微信:deeokay 电话:18940861882")

	//var arr []Question
	//engine.Find(&arr)
	//for _,v:=range arr{
	//	row,err:=engine.ID(v.Id).Cols("image").Update(Question{Image:fmt.Sprintf("%d.png",v.Id)})
	//	fmt.Println("更新ID图片",v.Id,row,err)
	//}
	//SQL段结束
}
